<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="挂牌名" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入挂牌名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型" prop="tradingType">
        <el-select v-model="queryParams.tradingType" placeholder="挂牌类型" clearable size="small">
          <el-option
            v-for="dict in tradingTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="auditDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="productId" width="100" />
      <el-table-column
        label="挂牌名"
        align="center"
        prop="productName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="挂牌类型"
        align="center"
        prop="tradingType"
        :formatter="tradingTypeFormat"
        width="100"
      />
      <el-table-column
        label="许可"
        align="center"
        prop="allowType"
        :formatter="allowTypeFormat"
        width="100"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="viewDetails(scope.row.productId)"
            v-hasPermi="['manage:audit_data_first:select']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="agree(scope.row.productId)"
            v-hasPermi="['manage:audit_data_first:agree']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="refuseView(scope.row.productId)"
            v-hasPermi="['manage:audit_data_first:refuse']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情弹窗 -->
    <el-dialog :title="title" :visible.sync="detailsOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="挂牌作品名" prop="dictName">
          <el-input v-model="form.worksName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="挂牌类型" prop="dictType">
          <el-input v-model="form.tradingType" placeholder="请输入挂牌类型" />
        </el-form-item>
        <el-form-item label="许可类型" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in allowTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 驳回填写信息弹窗-->
    <el-dialog :title="title" :visible.sync="refuseOpen" width="500px" append-to-body>
      <el-form ref="refuseForm" :model="refuseForm" :rules="refuseRules" label-width="80px">
        <el-form-item label="驳回原因" prop="auditExplain">
          <el-input v-model="refuseForm.auditExplain" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>

        <el-form-item prop="pictures" label="图片说明">
          <el-upload
            :action="uploadUrls"
            :headers="headers"
            :multiple = true
            list-type="picture-card"
            :with-credentials='true'
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-error="handleError"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRefuseForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listAuditData,getAuditData,agreeAuditData,refuseAuditData} from "@/api/manage/auditDataFirst";
  import Editor from '@/components/Editor';
  import { getToken } from "@/utils/auth";
  const baseUrl = process.env.VUE_APP_BASE_API;
  export default {
    name: "AuditDataFirst",
    components: {
      Editor
    },
    data() {
      return {
        headers: { Authorization: "Bearer " + getToken() },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 初审表格数据
        auditDataList: [],
        // 弹出层标题
        title: "",
        // 是否显示详情弹出层
        detailsOpen: false,
        // 是否显示驳回弹出层
        refuseOpen:false,
        // 挂牌类型字典
        tradingTypeOptions: [],
        // 许可类型字典
        allowTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          worksName: undefined,
          tradingType: undefined
        },
        // 查看详情表单参数
        form: {},
        rules: {
          dictName: [
            { required: true, message: "字典名称不能为空", trigger: "blur" }
          ],
          dictType: [
            { required: true, message: "字典类型不能为空", trigger: "blur" }
          ]
        },
        //驳回表单参数
        refuseForm:{},
        fileList:[],  //上传图片属猪
        refuseRules:{
          auditExplain: [
            { required: true, message: "驳回说明不能为空", trigger: "blur" }
          ]
        },
        //驳回图片的数组
        dialogImageUrl:'',
        dialogVisible:false,
        uploadUrls: baseUrl + '/common/upload'
      };
    },
    created() {
      console.log("进入了vue文件开始执行方法");
      this.getList();
      this.getDicts("trading_type").then(response => {
        this.tradingTypeOptions = response.data;
        console.log("挂牌类型");
        console.log(response.data);
      });
      this.getDicts("allow_type").then(response => {
        this.allowTypeOptions = response.data;
        console.log("专有类型");
        console.log(response.data);
      });
    },
    methods: {
      // 查询资料列表
      getList() {
        this.loading = true;
        listAuditData(this.queryParams).then(response => {
          console.log(response.rows);
          this.auditDataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      // 挂牌类型字典翻译
      tradingTypeFormat(row, column) {
        return this.selectDictLabel(this.tradingTypeOptions, row.tradingType);
      },
      // 许可类型字典翻译
      allowTypeFormat(row, column) {
        return this.selectDictLabel(this.allowTypeOptions, row.allowType);
      },
      // 取消按钮
      cancel() {
        this.detailsOpen = false;
        this.refuseOpen=false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          noticeId: undefined,
          noticeTitle: undefined,
          noticeType: undefined,
          noticeContent: undefined,
          status: "0"
        };
        this.refuseForm={};
        this.fileList=[];
        dialogImageUrl:'';
        dialogVisible:false;
        this.resetForm("form");
        this.resetForm("refuseForm");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.noticeId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 确认 */
      submitForm: function() {

      },

      /** 查看详情*/
      viewDetails(row){
        console.log(row);
        this.reset();
        const worksId = row;
        getAuditData(worksId).then(response => {
          this.form = response.data;
          this.detailsOpen = true;
          this.title = "审核挂牌详情";
        });
      },
      /** 初审通过*/
      agree(row){
        console.log('点击了通过');
        this.$confirm('该作品资料审核是否确认通过',"提示",{
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function(){
          console.log("确认");
          let data={productId:row}
          agreeAuditData(data);
        }).then(()=>{
          this.msgSuccess("通过成功");
        }).catch(function(){
          console.log("取消");
        })

      },
      /** 驳回*/
      refuseView(productId){
        this.refuseForm.productId=productId;
        this.reset();
        this.refuseOpen= true;
        this.title="驳回资料";
      },
      /** 提交驳回*/
      submitRefuseForm(){
        console.log("用户点击了提交驳回");
        this.$refs["refuseForm"].validate(valid =>{
          if(valid){
            let auditImage;
            if(this.fileList.length>1){ //上传了驳回说明的图片
              for(let i=0;i<this.fileList.length;i++){  //循环拼接
                if(i!=this.fileList.length){
                  auditImage=auditImage+this.fileList[i]+','
                }else{
                  auditImage=auditImage+this.fileList[i]
                }
              }
            }
            this.refuseForm.auditImage=auditImage;
            refuseAuditData(this.refuseForm).then(response=>{
              if(response.code==200){
               this.msgSuccess("驳回成功");
               this.refuseOpen=false;
               this.reset;
               this.getList();
              }
            });
          }
        })
      },

      handleRemove(file, fileList) {//移除
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {//预览
        console.log(file)
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleError(err, file, fileList){//上传失败
        console.log(err)
      },
      beforeAvatarUpload(file) {//文件上传之前调用做一些拦截限制
        console.log(file);
        const isJPG = true;
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      handleAvatarSuccess(res, file) {//图片上传成功
        console.log(res);
        var files = res.url;
        this.fileList.push(files)
        this.dialogImageUrl = res.url;
      }
    }
  };
</script>

<style scoped>

</style>
